package com.example.review.Fragment;


import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.review.Entity.LitePalUser;
import com.example.review.R;

import org.litepal.LitePal;
import org.litepal.crud.callback.FindMultiCallback;
import org.litepal.crud.callback.SaveCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class LitepalFragment extends Fragment {

    @BindView(R.id.checkbox)
    CheckBox checkbox;
    private View view;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.edit_password)
    EditText editPassword;
    @BindView(R.id.button1)
    Button button1;
    @BindView(R.id.button2)
    Button button2;
    @BindView(R.id.button3)
    Button button3;
    @BindView(R.id.button4)
    Button button4;
    @BindView(R.id.button5)
    Button button5;
    @BindView(R.id.button6)
    Button button6;
    @BindView(R.id.button7)
    Button button7;
    @BindView(R.id.button8)
    Button button8;
    @BindView(R.id.button9)
    Button button9;

    private SharedPreferences sp;
    public LitepalFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_litepal, container, false);
        ButterKnife.bind(this, view);
        sp = getActivity() .getSharedPreferences("sign",getActivity().MODE_PRIVATE);
        boolean sign = sp.getBoolean("sign", false);
        checkbox.setChecked(sign);
        return view;

    }

    @OnClick({R.id.button1, R.id.button2, R.id.button3, R.id.button4, R.id.button5, R.id.button6, R.id.button7, R.id.button8, R.id.button9})
    public void onViewClicked(View view) {
        String name = editName.getText().toString().trim();
        String password = editPassword.getText().toString();
        switch (view.getId()) {
            case R.id.button1:
                LitePalUser litePal = new LitePalUser();
                litePal.setName(name);
                litePal.setPasswrod(password);
                boolean save = litePal.save();
                Toast.makeText(getContext(), save + "", Toast.LENGTH_SHORT).show();
////                //批量添加
//                List<LitePalUser> list = new ArrayList<>();
//                for(int i=0;i<5;i++){
//                    LitePalUser litePal = new LitePalUser();
//                    litePal.setName("");
//                    litePal.setPasswrod("");
//                    list.add(litePal);
//                }
//                LitePal.saveAll(list);
                break;
            case R.id.button2:
//                //删除单个记录，id=1
//                int i1 = LitePal.delete(LitePalUser.class, 1);
//                //删除数据库中NewsBean表的所有记录
//                int i1 = LitePal.deleteAll(LitePalUser.class);
//                //删除数量大于100的
//                int i1 = LitePal.deleteAll(LitePalUser.class, "id > ?", "100");
                //删除标题为name的
                int i1 = LitePal.deleteAll(LitePalUser.class, "name = ?", name);
//                //删除标题为name，内容为password的记录
//                int i1 = org.litepal.LitePal.deleteAll(LitePalUser.class, "name = ? and password = ?", name, password);
                Toast.makeText(getContext(), "" + i1, Toast.LENGTH_SHORT).show();
                break;
            case R.id.button3:
//                //修改ID为1的记录，并将标题和内容分别设置为小铁，123456
//                LitePalUser litePalUser = LitePal.find(LitePalUser.class, 1);
//                litePalUser.setName("小铁");
//                litePalUser.setPasswrod("123456");
//                boolean save1 = litePalUser.save();
//                Toast.makeText(MainActivity.this,save1+"",Toast.LENGTH_SHORT).show();
//                //如下方式同上
                LitePalUser litePalUser1 = new LitePalUser();
                litePalUser1.setName("小铁");
                litePalUser1.setPasswrod("123456");
                //直接更新id为1的记录
                int update = litePalUser1.update(1);
                Toast.makeText(getContext(), update + "", Toast.LENGTH_SHORT).show();
//                //修改ID为1的记录
//                LitePalUser litePalUser2 = new LitePalUser();
//                //更新所有name为name的记录,更新为小铁
//                litePalUser2.setName("小铁");
//                litePalUser2.updateAll("name = ?", name);
                break;
            case R.id.button4:
                //将name为name，password为password的数据修改为小铁、123456
                LitePalUser litePalUser3 = new LitePalUser();
                litePalUser3.setName("小铁");
                litePalUser3.setPasswrod("123456");
                int i = litePalUser3.updateAll("name = ? and passwrod = ?", name, password);
                Toast.makeText(getContext(), "" + i, Toast.LENGTH_SHORT).show();
                break;
            case R.id.button5:
                //查询表中所有的记录，返回的是泛型为NewsBean的List集合
                List<LitePalUser> all = LitePal.findAll(LitePalUser.class);
                for (LitePalUser litePalUser : all) {
                    String name1 = litePalUser.getName();
                    Integer id = litePalUser.getId();
                    Log.v("TAG", id + "tgt" + name1);
                }

                //查找表id为1的记录
                LitePalUser litePalUser = LitePal.find(LitePalUser.class, 1);

                //获取表中的第一条数据与最后一条数据
                LitePalUser first = LitePal.findFirst(LitePalUser.class);
                LitePalUser last = LitePal.findLast(LitePalUser.class);

                //查询表中的第5、10、15条数据
                List<LitePalUser> all1 = LitePal.findAll(LitePalUser.class, 5, 10, 15);
                //或者定义一个数组
                long[] ids = new long[]{5, 10, 15};
                List<LitePalUser> all2 = LitePal.findAll(LitePalUser.class, ids);
                //查找name为name的记录,并且以时长作排序(按时间desc倒序  asc 正序)，where()方法接收任意个字符串参数，
                //其中第一个参数用于进行条件约束，从第二个参数开始，都是用于替换第一个参数中的占位符的。那这个where()方法就对应了一条SQL语句中的where部分。
                List<LitePalUser> time_desc = LitePal.where("name = ?", name).order("name desc").find(LitePalUser.class);
                //将查询出的按照的时间倒序排列，只要name和password这两列数据，即最新发布的放在最前面，那就可以这样写：
                LitePal.select("name", "passwrod")
                        .where("name > ?", name)
                        .order("name desc").find(LitePalUser.class);
                //设置查询的数量与偏移量
                List<LitePalUser> litePalUsers = LitePal.select("name", "passwrod")
                        .where("name > ?", name)
                        .order("name desc")
                        .limit(30)//只查询前面30条
                        .offset(20)//分页查询，每次查20条
                        .find(LitePalUser.class);
                break;
            case R.id.button6:
                break;
            case R.id.button7:
                break;
            case R.id.button8:
                break;
            case R.id.button9:
                break;
        }
    }

    private void syncQuery() {
        //1、 异步查询示例
        //使用findallasync() 代替 findall() ，然后调用 listen() 方法，查询完成就会回调onFinish(）方法
        LitePal.findAllAsync(LitePalUser.class).listen(new FindMultiCallback<LitePalUser>() {
            @Override
            public void onFinish(List<LitePalUser> allSongs) {
                //查询结果
            }
        });
        //2、异步保存示例
        LitePalUser litePalUser1 = new LitePalUser();
        litePalUser1.setName("小铁");
        litePalUser1.setPasswrod("123456");
        litePalUser1.saveAsync().listen(new SaveCallback() {
            @Override
            public void onFinish(boolean success) {

            }
        });
    }

    @OnClick(R.id.checkbox)
    public void onViewClicked() {
        SharedPreferences.Editor edit = sp.edit();
        edit.putBoolean("sign", checkbox.isChecked());
        boolean commit = edit.commit();
        Log.v("TAG","保存"+commit);
    }
}
